package com.fyzk.activity.user;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.fyzk.activity.app.App;
import com.fyzk.base.BaseActivity;
import com.fyzk.activity.app.AppToast;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.net.PublicMethod;
import com.fyzk.utils.AppUtil;
import com.tyjc.lottery.R;
import com.fyzk.net.Commands;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.view.TopTitleView;

import org.json.JSONException;
import org.json.JSONObject;

import http.Common.CommandUtil;
import http.StringResultBean;

public class BindAlipayActivity extends BaseActivity {

    private String userName;
    private String realName;

    private TextView tv_userName;

    private EditText et_yinNum;
    private EditText et_yinOwer;

    private TextView tiJiao;

    private TextView txServiceQQ;

    private TopTitleView bangdingalipay_top_rl;

    //客服QQ
    private String serviceQQ = "";

    //=======================================================
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_alipay);
        bangdingalipay_top_rl = (TopTitleView) findViewById(R.id.bangdingalipay_top_rl);
        bangdingalipay_top_rl.setOnTitleClickListener(this);

        userName = getIntent().getStringExtra(IntentParamUtils.USERNAME);
        realName = getIntent().getStringExtra(IntentParamUtils.REALNAME);

        info();
    }

    //
    // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    /**
     * 请求绑定支付宝
     */
    private void requestBindZfb() {
        JSONObject request_json = new JSONObject();
        try {
            request_json.put("bindCard", et_yinNum.getText().toString());
            httpPostJson(CommandUtil.BINDALIPAY, Commands.BINDZFB, request_json, true);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        AppToast.getToast().show("绑定成功");
        finish();
    }

    @Override
    protected void onFailure(int requestCode, String errorCode, String message) {
//		super.onFailure(requestCode, errorCode, message);
        if (errorCode.equals("40002")) {
            AppToast.getToast().show("用户不存在");
        } else if (errorCode.equals("40014")) {
            AppToast.getToast().show("真实姓名没填写");
        } else if (errorCode.equals("40015")) {
            AppToast.getToast().show("该卡号已绑定");
        } else {
            AppToast.getToast().show("网络异常");
        }
    }

    //
    // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    private void info() {

        tv_userName = (TextView) findViewById(R.id.bangdingalipay_username);
        et_yinNum = (EditText) findViewById(R.id.bangdingalipay_yinnum);
        et_yinOwer = (EditText) findViewById(R.id.bangdingalipay_realname);
        et_yinOwer.setText(PublicMethod.tv_yinCang(realName, 1,
                0));
        et_yinOwer.setEnabled(false);
        tiJiao = (TextView) findViewById(R.id.bangdingalipay_btn_bangding);

        tv_userName.setText(userName);

        tiJiao.setOnClickListener(this);

        txServiceQQ = (TextView) findViewById(R.id.txServiceQQ);

        serviceQQ = MySharedPreferences.getInstance().getServiceQQ();
        String arrayQQ = AppUtil.getFormatStr(
                App.getContext().getResources().getString(R.string.service_qq),
                serviceQQ);
        txServiceQQ.setText(arrayQQ);
    }

    /**
     * 非空验证
     */
    private boolean isNull(EditText editText) {
        if (editText.getText() == null || editText.getText().toString().equals("")) {
            editText.requestFocus();
            return true;
        }
        return false;
    }

    private boolean et_yanZheng() {

        if (isNull(et_yinNum)) {
            AppToast.getToast().show("支付宝帐号不能为空！");
            return false;
        }

//		if( !Pattern.matches("\\d{19}", et_yinNum.getText().toString())){
//			AppToast.getToast().show("卡号位数不正确！");
//			et_yinNum.requestFocus();
//			et_yinNum.setSelectAllOnFocus(true);
//			return false;
//		}

        return true;
    }


    /**
     * 绑定支付宝
     */
    private void bindAlipay(int schemaDef) {
//		ProtocalEngine engine = new ProtocalEngine(this);
//		BindAlipayRequestData request = new BindAlipayRequestData();
//		request.bankCard = et_yinNum.getText().toString();
//
//		engine.request(this, schemaDef, request);

    }


    /**
     * 单击事件
     *
     * @author Administrator
     */
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {
            case R.id.bangdingalipay_btn_bangding:

                if (et_yanZheng()) {
                    requestBindZfb();
                }

                break;

            default:
                break;
        }
    }

}
